﻿using AbpApplication.Expand;
using AbpApplication.InPut;
using AbpEntityFrameworkcore.DbContext;
using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using LinqSharp;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wms_wpf.Apiservice;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AbpApplication.AppServices
{
    public class ViewService : ApplicationService, IViewService
    {


        private readonly MyDbContext context;
        private readonly IRepository<Box> Box_res;
         private readonly IRepository<BoxGrid> BoxGrid_res;
        private readonly  IRepository<Warehouse> Warehouse_res;
        private readonly ILoginHelper loginHelper;
        private readonly QueryDynamic queryDynamic;
        public ViewService(MyDbContext context, IRepository<Box> box_res, IRepository<BoxGrid> boxGrid_res, IRepository<Warehouse> warehouse_res, ILoginHelper loginHelper, QueryDynamic queryDynamic)
        {
            this.context = context;
            Box_res = box_res;
            BoxGrid_res = boxGrid_res;
            Warehouse_res = warehouse_res;
            this.loginHelper = loginHelper;
            this.queryDynamic = queryDynamic;
        }
  
        public async Task<ApiResponse> View_WareHouse()
        {
            try
            {            
                List<Warehouse> warehouses = await Warehouse_res.GetListAsync();
                return new ApiResponse("获取成功", true, warehouses);
            }
            catch (Exception ex)
            {
                Logger.LogError("View_WareHouse错误");
                return new ApiResponse("View_WareHouse错误", false,null);
            }
        }
    
        public async Task<ApiResponsePage>View_Material(InPut_ViewService4 InPut)
        {
           List<View_Material> data=  queryDynamic.QueryDynameicView_Material(InPut.BoxCode, InPut.BoxGridCode, InPut.MaterialName,
                                                    InPut.MaterialCode, InPut.MaterialProertyname);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);

        }
   
        public async Task<ApiResponsePage> View_Box_WarehouseLine(InPut_ViewService6 InPut)
        {
            List<View_Box_WarehouseLine> data=  queryDynamic.QueryDynameicView_Box_WarehouseLine(InPut.BoxCode, InPut.WarehouseLineCode);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponsePage> View_NeetOutMaterial(InPut_ViewService10 InPut)
        {
            List<View_NeetOutMaterial> data = queryDynamic.QueryDynameicView_View_NeetOutMaterial(InPut.MaterialName,InPut.MaterialCode,InPut.BoxCode);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponsePage> View_LogNote(InPut_ViewService7 InPut)
        {
           List<lognote> data=queryDynamic.QueryDynameicView_View_Lognote(InPut.Type,InPut.StartTime,InPut.EndTime,InPut.Note,
               InPut.User,InPut.Definition,InPut.MaterialCode,InPut.MaterialName,InPut.Batch,InPut.Size,InPut.OrderNo,InPut.BoxCode,InPut.WarehouseLineCode);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponsePage> View_MaterialProperty(InPut_ViewService8 InPut)
        {
            List<MaterialProperty> data=   queryDynamic.QueryDynameic_MaterialProperty(InPut.MaterialProertyname, InPut.Type, InPut.Description);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }


        //已用
        public async Task<ApiResponsePage> View_Box(InPut_ViewService InPut)
        {
            try
            {
                IQueryable<View_Box> view_Boxequery = context.viewBox;
                if (!string.IsNullOrEmpty(InPut.BoxCode))
                {
                    view_Boxequery = view_Boxequery.Where(x => x.BoxCode.Contains(InPut.BoxCode));
                }
                List<View_Box> view_Boxes = view_Boxequery.ToList();
                long count = view_Boxes.LongCount();//计算出了总条数
                long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
                view_Boxes = view_Boxes.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
                return new ApiResponsePage("获取成功", true, view_Boxes, count, pageCount);
            }
            catch (Exception ex)
            {
                loginHelper.LoginAsync(InPut, " View_Box");
                return new ApiResponsePage("View_Box错误", false, null, null, null);
            }
        }
        public async Task<ApiResponsePage> View_BoxGrid_Material(InPut_ViewService2 InPut)
        {
            var boxgrids = await BoxGrid_res.WithDetailsAsync();
            if (!string.IsNullOrEmpty(InPut.BoxCode))
            {
                boxgrids = boxgrids.Where(x => x.TheBox.BoxCode == InPut.BoxCode);
            }
            long count = boxgrids.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            var res = boxgrids.Select
                (x => new {
                    BoxGridCode = x.BoxGridCode,
                    CreateTime = x.TheBox.CreateTime,
                    UpdateTime = x.TheBox.UpdateTime,
                    BoxPositionQuantity=x.BoxPositionQuantity

                });
            res = res.SelectPage(InPut.PageIndex, InPut.PageSize);
            return new ApiResponsePage("获取成功", true, res.ToList(), count, pageCount);
        }
        public async Task<ApiResponsePage> View_WareHouseLine(InPut_ViewService3 InPut)
        {
            List<WarehouseLine> data = queryDynamic.QueryDynameic(InPut.Xaxis, InPut.Yaxis, InPut.Zaxis, InPut.Id_Warehouse);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponsePage> View_TaskOrder(InPut_ViewService5 InPut)
        {
            List<View_TaskOrder> data = queryDynamic.QueryDynameicView_TaskOder(InPut.Type, InPut.OrderNo, InPut.BoxCode, InPut.State,
                                                     InPut.WarehouseCode, InPut.InlethouseCode, InPut.TaskOrderCode,
                                                     InPut.groupPriority, InPut.taskPriority);
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / InPut.PageSize);
            data = data.SelectPage(InPut.PageIndex, InPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponsePage> View_Material_WarehouseLine(InPut_ViewService9 inPut)
        {
            List<View_Material_WarehouseLine> data = queryDynamic.QueryDynameicView_Material_WarehouseLine(inPut.MaterialName, inPut.MaterialCode,
                inPut.MaterialProertyname, inPut.Type, inPut.Description, null, inPut.BoxCode, inPut.WarehouseLineCode);
            data = data.OrderBy(x => x.CreatedDate).ToList();
            long count = data.LongCount();//计算出了总条数
            long pageCount = (long)Math.Ceiling(count * 1.0 / inPut.PageSize);
            data = data.SelectPage(inPut.PageIndex, inPut.PageSize).ToList();
            return new ApiResponsePage("获取成功", true, data, count, pageCount);
        }
        public async Task<ApiResponse> View_Inlethouse(string InlethouseCode)
        {
          List<InlethouseLine> inlethouseLines=   await context.inlethousesLine.Include(x=>x.Box).      
                Where(x=>x.InlethouseLineCode.Contains(InlethouseCode)). ToListAsync();
            inlethouseLines= inlethouseLines.GroupBy(x=>x.InlethouseLineCode.Substring(x.InlethouseLineCode.Length-1)).
          OrderByDescending(group=>group.Key).Select(group => group
                .OrderBy(x=>x.InlethouseLineCode.Substring(x.InlethouseLineCode.Length-8))  
                .ToList())
            .SelectMany(group => group) // 展开分组  
            .ToList();


            var inlethouselineDto=       inlethouseLines.Select(x => new {x.Box?.BoxCode , x.InlethouseLineCode,x.IsOccupy}).ToList();
            return new ApiResponse("获取成功",true, inlethouselineDto);

        }
    }
}
